package com.yenlien.traceyun.plant.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.yenlien.traceyun.business.entity.Equipment;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.plant.dao.SoilDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.interceptor.DataAuth;
import com.yenlien.traceyun.plant.entity.Soil;
import com.yenlien.traceyun.plant.service.SoilService;
import com.yenlien.traceyun.webase.controller.BaseController;

/**
 * 数字地图
 *
 * @author maybe
 * @date 2020年7月23日
 */
@RestController
@RequestMapping("/soil")
public class SoilController extends BaseController {
    @Autowired
    private SoilService soilService;

    @Autowired
    private SoilDao soilDao;

    //分页查询列表
    @DataAuth
    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody JSONObject params) {
        Soil soil = new Soil();
        soil.setPage(params.getIntValue("page"));
        soil.setType(params.getIntValue("type"));
        Page<Soil> page = soilService.findByList(soil);
        if (page.getContent() != null && page.getContent().size() > 0) {
            List<String> pids = new ArrayList<>();
            for (Soil item : page.getContent()) {
                if (!StringUtils.isEmpty(item.getPid())) {
                    pids.add(item.getPid());
                }
            }
            List<Soil> parents = soilDao.findById(pids);
            if (parents != null && parents.size() > 0) {
                for (Soil item : page.getContent()) {
                    for (Soil parent : parents) {
                        if (parent.getId().equals(item.getPid())) {
                            item.setpName(parent.getName());
                            break;
                        }
                    }
                }

            }
        }
        return buildPage(soilService.findByList(soil));
    }

    //根据ID查询详情
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable("id") String id) {
        return success(soilService.findById(id));
    }

    //新增(formdata提交)
//	@RequestMapping(value="/save", method = RequestMethod.POST)
//	public Object save(Soil soil, HttpServletRequest request) throws Exception {
//		List<MultipartFile> files = new ArrayList<>();
//	    if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
//	        files = ((MultipartHttpServletRequest) request).getFiles("files");
//	    }
//	    soilService.save(soil, files);
//		return success("success");
//	}

    //新增
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(Soil soil, HttpServletRequest request) throws Exception {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        soilService.save(soil, files);
        return success("success");
    }

    //修改
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(Soil soil, HttpServletRequest request) throws Exception {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        soilService.update(soil, files);
        return success("success");
    }

    //假删除
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.GET)
    public Object deleteById(@PathVariable("id") String id) {
        soilService.deleteById(id);
        return success("success");
    }

    //查询基地下拉
    @DataAuth
    @RequestMapping(value = "/findAllBase", method = RequestMethod.GET)
    public Object findAllBase() {
        return success(soilService.findAllBase());
    }

    //根据基地ID查询地块
    @RequestMapping(value = "/findByBaseId/{baseId}")
    public Object findByBaseId(@PathVariable("baseId") String baseId) {
        return success(soilService.findByBaseId(baseId));
    }

    //根据类型查询下拉
    @DataAuth
    @RequestMapping(value = "/findSoilByType", method = RequestMethod.POST)
    public Object findSoilByType(@RequestBody JSONObject params) {
        return success(soilService.findSoilByType(params.getString("type")));
    }

    //查询所有地块
    @DataAuth
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Object findAll() {
        return success(soilService.findAll());
    }

    //绑定地块设备信息
    @RequestMapping(value = "/bind", method = RequestMethod.POST)
    public Object bind(@RequestBody JSONObject params) {
        soilService.bind(params);
        return success("success");
    }

    @RequestMapping(value = "/equiment/update", method = RequestMethod.POST)
    public Object updateEquipment(@RequestBody Equipment equipment) {
        soilService.updateEquipment(equipment);
        return success("ok");
    }

    @RequestMapping(value = "/equiment/dele/{id}", method = RequestMethod.GET)
    public Object deleteEquipment(@PathVariable("id") String id) {
        soilService.deleteEquipment(id);
        return success("ok");
    }

    /**
     * 查询所有地块数量
     *
     * @return
     */
    @PostMapping("/soils")
    public Object soils() {
        return success(soilService.soils(UserAuthUtil.getLoginUser().getCompanyId()));
    }

    /**
     * 查询公司地块数量
     *
     * @return
     */
    @PostMapping("/soil")
    public Object soil(@RequestBody JSONObject param) {
        String companyId = param.getString("companyId");
        return success(soilService.soil(companyId));
    }


}
